package com.hopu.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hopu.domain.Factoryc;
import com.hopu.service.FactorycService;

@Controller
@RequestMapping("/factory")
public class FactorycController {
    @Autowired
    private FactorycService factorycService;

    @RequestMapping("/main")
    public String main() {
        return "/baseinfo/main";
    }


    @RequestMapping("/toview")
    public String toview(String[] id, Model model) {
        System.out.println("测试");
        if (id != null && id.length == 1) {
            for (String fid : id) {
                Factoryc factoryc = factorycService.findById(fid);
                model.addAttribute("obj", factoryc);
            }
            return "/basicinfo/factory/jFactoryView";
        } else {
            return "redirect:list";
        }
    }


    @RequestMapping("/tocreate")
    public String tocreate() {
        return "/basicinfo/factory/jFactoryCreate";
    }


    @RequestMapping("/toupdate")
    public String toupdate(String[] id, Model model) {
        if (id != null && id.length == 1) {
            for (int i = 0; i < id.length; i++) {
                Factoryc factoryc = factorycService.findById(id[i]);
                model.addAttribute("obj", factoryc);
            }
            return "/basicinfo/factory/jFactoryUpdate";
        } else {
            return "redirect:list";
        }
    }

    @RequestMapping("/deleteById")
    public String deleteById(String[] id) {
        if (id != null && id.length == 1) {
            for (String fid : id) {
                factorycService.delete(fid);
            }
            return "redirect:list";
        } else {
            return "redirect:list";
        }
    }


    @RequestMapping("/delete")
    public String delete(String[] id) {
        if (id != null) {
            for (String fid : id) {
                factorycService.delete(fid);
            }
        }
        return "redirect:list";
    }


    @RequestMapping("/start")
    public String start(String[] id) {
        if (id != null) {
            for (String fid : id) {
                factorycService.startState(fid);
            }
        }
        return "redirect:list";
    }


    @RequestMapping("/stop")
    public String stop(String[] id) {
        if (id != null) {
            for (String fid : id) {
                factorycService.stopState(fid);
            }
        }
        return "redirect:list";
    }


    @RequestMapping("/list")
    public String findAll(Model model) {
        List<Factoryc> list = factorycService.findAll();
        System.out.println(list.size());
        model.addAttribute("dataList", list);
        return "/basicinfo/factory/jFactoryList";
    }


    @RequestMapping("/insert")
    public String insert(Factoryc factoryc) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        factoryc.setId(uuid);
        factoryc.setCtype(1);
        factoryc.setState("1");
        factoryc.setCreateBy("吴勇");

        factoryc.setCreateDept("开发部");
        factoryc.setCreateTime(Date.valueOf(df.format(new java.util.Date())));
        factorycService.insert(factoryc);
        return "redirect:list";
    }


    @RequestMapping("/update")
    public String update(Factoryc factoryc) {
        factorycService.update(factoryc);
        return "redirect:list";
    }

}